/* eslint-disable */
/**
 * @name 返回一个在所有给定的promise都已经fulfilled或rejected后的promise，并带有一个对象数组，每个对象表示对应的promise结果
 * @param {promise} promises
 * @returns promise
 */
const allSettled = function allSettled(promises) {
  let _resolve;
  const retPromise = new Promise(resolve => {
    _resolve = resolve;
  });
  let index = 0;
  let count = 0;
  const ret = [];
  for (const promise of promises) {
    const curIndex = index++;
    Promise.resolve(promise).then(
      res => {
        count++;
        ret[curIndex] = {
          status: FULLFILLED,
          value: res,
        };
        if (count === promises.length) {
          _resolve(ret);
        }
      },
      error => {
        count++;
        ret[curIndex] = {
          status: REJECTED,
          value: error,
        };
        if (count === promises.length) {
          _resolve(ret);
        }
      }
    );
  }
  return retPromise;
};
export default allSettled;
